package com.he1618.boot3.app;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.EuclideanDistanceSimilarity;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.impl.similarity.UncenteredCosineSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.model.JDBCDataModel;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

public class RecommenderExample {
    public static void main(String[] args) {
        try {
            BasicDataSource dataSource = new BasicDataSource();
            dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://192.168.1.108:3306/demo");
            dataSource.setUsername("root");
            dataSource.setPassword("root");

            JDBCDataModel dataModel = new MySQLJDBCDataModel(dataSource, "rating_table", "user_column", "item_column", "preference_column", "timestamp_column");


            // 加载数据模型
            //DataModel model = new FileDataModel(new File("E:\\data\\a.csv"));

            // 创建用户相似性计算器，这里使用皮尔逊相关系数
            UserSimilarity similarity = new EuclideanDistanceSimilarity(dataModel);

            // 输出相似性矩阵
            System.out.println("Similarity Matrix:");
            System.out.println(similarity);

            // 创建用户邻域，选择与用户最相似的前N个用户
            NearestNUserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, dataModel);

            // 输出邻域信息
            System.out.println("Neighborhood:");
            System.out.println(neighborhood);

            // 创建用户推荐器
            GenericUserBasedRecommender recommender = new GenericUserBasedRecommender(dataModel, neighborhood, similarity);

            // 输出用户评分信息
            // 输出用户评分信息
            for (LongPrimitiveIterator it = dataModel.getUserIDs(); it.hasNext(); ) {
                Object userID = it.next();
                System.out.println("userId:"+userID);
                for (org.apache.mahout.cf.taste.model.Preference pref : dataModel.getPreferencesFromUser((Long) userID)) {
                    System.out.println("  Item " + pref.getItemID() + ": " + pref.getValue());
                }

            }

            for (int i = 1  ; i < 6; i++) {
                System.out.println("************************");
                long[] users = recommender.mostSimilarUserIDs(i, 2);
                for (long user : users) {
                    System.out.println(user);
                }

                // 假设用户ID为1，获取对该用户的推荐物品
                List<RecommendedItem> recommendations = recommender.recommend(i, 5,true);

                // 打印推荐结果
                System.out.println("Recommendations for User 1:");
                for (RecommendedItem recommendation : recommendations) {
                    System.out.println("Item ID: " + recommendation.getItemID() + ", Score: " + recommendation.getValue());
                }
            }


        } catch (TasteException e) {
            e.printStackTrace();
        }
    }
}
